﻿using System;
using System.Collections.Generic;

namespace SimpleMessageLibrary
{
    // 消息基类
    public abstract class MessageBase
    {
        public string MessageId { get; set; }
        public DateTime Timestamp { get; set; }
        public string Sender { get; set; }
        public string Receiver { get; set; }

        public MessageBase()
        {
            MessageId = Guid.NewGuid().ToString();
            Timestamp = DateTime.UtcNow;
        }
    }

    // 文本消息
    public class TextMessage : MessageBase
    {
        public string Content { get; set; }
    }

    // 命令消息
    public class CommandMessage : MessageBase
    {
        public string CommandName { get; set; }
        public Dictionary<string, string> Parameters { get; set; }
    }

    // 日志级别枚举
    public enum LogLevel
    {
        Debug,
        Info,
        Warning,
        Error
    }

    // 日志接口
    public interface ILogger
    {
        void Log(string message, LogLevel level = LogLevel.Info);
        void Log(string message, Exception ex, LogLevel level = LogLevel.Error);
    }

    // 控制台日志实现
    public class ConsoleLogger : ILogger
    {
        public void Log(string message, LogLevel level = LogLevel.Info)
        {
            Console.WriteLine($"[{level}] {DateTime.Now:yyyy-MM-dd HH:mm:ss} - {message}");
        }

        public void Log(string message, Exception ex, LogLevel level = LogLevel.Error)
        {
            Console.WriteLine($"[{level}] {DateTime.Now:yyyy-MM-dd HH:mm:ss} - {message}");
            Console.WriteLine($"  异常: {ex.Message}");
            Console.WriteLine($"  堆栈: {ex.StackTrace}");
        }
    }

    // 消息处理器委托
    public delegate void MessageHandler(MessageBase message);

    // 消息管理器
    public class MessageManager
    {
        private readonly ILogger _logger;
        private readonly Dictionary<Type, List<MessageHandler>> _handlers = new Dictionary<Type, List<MessageHandler>>();

        // 单例实现
        public static MessageManager Default { get; } = new MessageManager(new ConsoleLogger());

        public MessageManager(ILogger logger)
        {
            _logger = logger;
        }

        // 注册消息处理器
        public void RegisterHandler<T>(MessageHandler handler) where T : MessageBase
        {
            var messageType = typeof(T);
            if (!_handlers.ContainsKey(messageType))
            {
                _handlers[messageType] = new List<MessageHandler>();
            }
            _handlers[messageType].Add(handler);
            _logger.Log($"注册消息处理器: {messageType.Name}");
        }

        // 发送消息
        public void SendMessage(MessageBase message)
        {
            if (message == null)
            {
                _logger.Log("尝试发送空消息", LogLevel.Warning);
                return;
            }

            _logger.Log($"发送消息: {message.GetType().Name}, ID: {message.MessageId}");

            var messageType = message.GetType();
            if (_handlers.TryGetValue(messageType, out var handlers))
            {
                foreach (var handler in handlers)
                {
                    try
                    {
                        handler(message);
                    }
                    catch (Exception ex)
                    {
                        _logger.Log($"处理消息时出错: {messageType.Name}", ex, LogLevel.Error);
                    }
                }
            }
            else
            {
                _logger.Log($"没有找到消息 {messageType.Name} 的处理器", LogLevel.Warning);
            }
        }

        // 日志方法
        public void LogDebug(string message) => _logger.Log(message, LogLevel.Debug);
        public void LogInfo(string message) => _logger.Log(message, LogLevel.Info);
        public void LogWarning(string message) => _logger.Log(message, LogLevel.Warning);
        public void LogError(string message, Exception ex = null) => _logger.Log(message, ex, LogLevel.Error);
    }
}